package com.a3g.findnumber;

import java.util.List;

import com.a3g.findnumber.entity.MusicService;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Vibrator;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;


/**
 * @author Administrator
 *
 */
public class StartAct extends Activity implements OnClickListener {

	private Button musicStatusButton;

	private Button speakerButton;

	private Button playButton;

	public static boolean isSpeakerMute = false;

	public static boolean isMusicMute = false;

	public Vibrator vibrator;

	private Boolean exitStatus = false;

	MediaPlayer mClickSpeaker;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.game_start);
		setVolumeControlStream(AudioManager.STREAM_MUSIC);

		findView();

		// Background Music
		if (isMusicMute == false) {
			musicStatusButton.setBackgroundResource(R.drawable.play);
			startService(new Intent(this, MusicService.class));
		} else {
			musicStatusButton.setBackgroundResource(R.drawable.stop);
			stopService(new Intent(this, MusicService.class));
		}

		// Click Music
		if (isSpeakerMute == false) {
			speakerButton.setBackgroundResource(R.drawable.speaker_on);

		} else {
			speakerButton.setBackgroundResource(R.drawable.speaker_off);

		}

		setListener();

	}

	@Override
	public void onClick(View v) {
		int code = v.getId();

		switch (code) {
		case R.id.musicButton: {
			isMusicMute = !isMusicMute;
			if (isMusicMute == false) {
				musicStatusButton.setBackgroundResource(R.drawable.play);
				startService(new Intent(this, MusicService.class));
			} else {
				musicStatusButton.setBackgroundResource(R.drawable.stop);
				stopService(new Intent(this, MusicService.class));
			}

			break;

		}

		case R.id.speakerButton: {
			isSpeakerMute = !isSpeakerMute;
			if (isSpeakerMute == true) {
				speakerButton.setBackgroundResource(R.drawable.speaker_off);
				// Release any resources from previous MediaPlayer
				if (mClickSpeaker != null) {
					mClickSpeaker.release();
				}

			} else {
				speakerButton.setBackgroundResource(R.drawable.speaker_on);
				// Release any resources from previous MediaPlayer
				if (mClickSpeaker != null) {
					mClickSpeaker.release();
				}

				// Create a new MediaPlayer to play this sound
				mClickSpeaker = MediaPlayer
						.create(this, R.raw.true_click_music);
				mClickSpeaker.start();
			}

			break;

		}

		case R.id.playButton: {
			createPlaySession(R.drawable.bg_star_blue);
			break;
			
		}

		default:
			break;
		}

	}

	private void createPlaySession(int selectedColor) {
		Intent choiceLevelIntent = new Intent(getApplicationContext(), ChoiceLevelAct.class);
		startActivity(choiceLevelIntent);
	}

	private void findView() {
		musicStatusButton = (Button) findViewById(R.id.musicButton);
		speakerButton = (Button) findViewById(R.id.speakerButton);
		playButton = (Button) findViewById(R.id.playButton);
	}

	private void setListener() {
		musicStatusButton.setOnClickListener(this);
		speakerButton.setOnClickListener(this);
		playButton.setOnClickListener(this);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();

	}

	@Override
	protected void onResume() {
		super.onResume();

	}

	@Override
	public void onBackPressed() {
		if (exitStatus) {
			stopService(new Intent(this, MusicService.class));
			StartAct.this.finish();
			System.exit(0);
		} else {
			Toast.makeText(this, "Press Back again to Exit.",
					Toast.LENGTH_SHORT).show();
			exitStatus = true;
			new Handler().postDelayed(new Runnable() {
				@Override
				public void run() {
					exitStatus = false;
				}
			}, 3 * 1000);
		}
	}

	@Override
	public void onPause() {
		if (isApplicationSentToBackground(this)) {
			// Stop music when Home Key being Pressed
			stopService(new Intent(this, MusicService.class));
		}
		super.onPause();
	}

	public static boolean isApplicationSentToBackground(final Context context) {
		ActivityManager am = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningTaskInfo> tasks = am.getRunningTasks(1);
		if (!tasks.isEmpty()) {
			ComponentName topActivity = tasks.get(0).topActivity;
			if (!topActivity.getPackageName().equals(context.getPackageName())) {
				return true;
			}
		}
		return false;
	}

}
